﻿using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using System.Web.Http;
using Microsoft.AspNet.Identity;
using SDCloudSolution.SDCloud.BusinessLayer.Identity;
using SDCloudSolution.SDCloud.WebApi.Entities;
using SDCloudSolution.SDCloud.WebApi.Exceptions;

namespace SDCloudSolution.SDCloud.WebApi.Controllers
{
    /// <summary>
    /// Provides actions for handling an profile specific requests.
    /// </summary>
    [RoutePrefix("account")]
    [Authorize]
    public class AccountController : ApiController
    {
        private readonly ApplicationUserManager _userManager;

        /// <summary>
        /// Initializes a new instance of the <see cref="AccountController"/> class.
        /// </summary>
        /// <param name="userManager">The user manager.</param>
        public AccountController(ApplicationUserManager userManager)
        {
            _userManager = userManager;
        }

        /// <summary>
        /// Returns a profile information of current authorized user.
        /// </summary>
        /// <returns></returns>
        [Route("v1")]
        [HttpGet]
        public async Task<ProfileInfoRestModel> ProfileInfo()
        {
            var user = await _userManager.FindByIdAsync(User.Identity.GetUserId<int>());

            return new ProfileInfoRestModel
            {
                Name = user.UserName,
                RegistrationDate = user.RegistrationDateUtc.Date
            };
        }

        [Route("{id}/v1")]
        [HttpGet]
        public void GetUserHistory(int id)
        {
            // TODO Remove
            throw new RequiredParameterWebApiException("id");
        }
    }
}